package cc.yugu.yhdw.webserver.service.hdw;

import cc.yugu.yhdw.common.domain.hdw.BatteryReportLog;
import cc.yugu.yhdw.common.domain.hdw.ExchangeOrderBatteryReportLog;
import cc.yugu.yhdw.common.entity.LocationInfo;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import cc.yugu.yhdw.common.entity.pagination.Page;
import cc.yugu.yhdw.common.utils.YhdwUtils;
import cc.yugu.yhdw.webserver.persistence.hdw.ExchangeOrderBatteryReportLogMapper;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 换电电池上报记录
 *
 * @Date:Created in 2018/7/10 14:30
 * @Author:
 * @Modified By: zxj 2018/7/10 14:30(修改定位坐标系-数据库中的经纬度转换为百度地图坐标系）
 */
@Service
public class ExchangeOrderBatteryReportLogService {
    @Autowired
    ExchangeOrderBatteryReportLogMapper batteryOrderBatteryReportLogMapper;
    /**
     * 点击获取地址业务（转换）
     * @Param [search]
     * @Return cc.yugu.yhdw.common.entity.pagination.Page
     * @Author zxj 2018/7/10
     */
    public Page findselectPage(ExchangeOrderBatteryReportLog search) {
        Page page = search.buildPage();
        if (StringUtils.isNotEmpty(batteryOrderBatteryReportLogMapper.findTableExist(ExchangeOrderBatteryReportLog.EXCHANGE_ORDER_BATTERY_REPORT_LOG + search.getSuffix()))) {
            if (search.getQueryBeginTime() == null && search.getQueryEndTime() == null) {
                Date queryBeginTime = batteryOrderBatteryReportLogMapper.findLastReportTime(search.getOrderId(), search.getSuffix());
                queryBeginTime.setHours(0);
                queryBeginTime.setMinutes(0);
                queryBeginTime.setSeconds(0);
                search.setQueryBeginTime(queryBeginTime);
            }
            page.setTotalItems(batteryOrderBatteryReportLogMapper.findAllMapCount(search.getOrderId(), search.getSuffix(), search.getQueryBeginTime(), search.getQueryEndTime()));
            search.setBeginIndex(page.getOffset());
            // page.setResult(batteryOrderBatteryReportLogMapper.findSelectPageResult(search));
            // 坐标系转换
            List<ExchangeOrderBatteryReportLog> list =batteryOrderBatteryReportLogMapper.findSelectPageResult(search);
            List<ExchangeOrderBatteryReportLog> exchangeOrderBatteryReportLogs=new ArrayList<ExchangeOrderBatteryReportLog>();
            // 坐标经纬度处理
            for (ExchangeOrderBatteryReportLog exchangeOrderBatteryReportLog : list) {
                // 坐标系转换
                double[] d = YhdwUtils.gspToBd(exchangeOrderBatteryReportLog.getLng(), exchangeOrderBatteryReportLog.getLat());
                exchangeOrderBatteryReportLog.setLng(d[0]);
                exchangeOrderBatteryReportLog.setLat(d[1]);
                exchangeOrderBatteryReportLogs.add(exchangeOrderBatteryReportLog);
            }
            page.setResult(exchangeOrderBatteryReportLogs);
        }
        return page;
    }
    /**
     * 初始订单轨迹定位业务（转换）
     * @Param [orderId, createTime, startDate, endDate]
     * @Return java.util.List<cc.yugu.yhdw.common.entity.LocationInfo>
     * @Author zxj 2018/7/10
     */
    public List<LocationInfo> findAllMap(String orderId, Date createTime, Date startDate, Date endDate) {
        String suffix = DateFormatUtils.format(createTime, "yyyyww");
        List<LocationInfo> result = new ArrayList<LocationInfo>();
        List<LocationInfo> results = new ArrayList<LocationInfo>();
        if (startDate == null && endDate == null) {
            startDate = batteryOrderBatteryReportLogMapper.findLastReportTime(orderId, suffix);
            startDate.setHours(0);
            startDate.setMinutes(0);
            startDate.setSeconds(0);
        }
        if (StringUtils.isNotEmpty(batteryOrderBatteryReportLogMapper.findTableExist(ExchangeOrderBatteryReportLog.EXCHANGE_ORDER_BATTERY_REPORT_LOG + suffix))) {
            result = batteryOrderBatteryReportLogMapper.findAllMap(orderId, suffix, startDate, endDate);

            // 添加对地图坐标不同的处理（电池上传日志的坐标和百度地图的坐标是不一样）
            for (LocationInfo locationInfo : result) {
                // 坐标系转换(未做经纬度为空的校验）
                double[] d = YhdwUtils.gspToBd(locationInfo.getLng(), locationInfo.getLat());
                locationInfo.setLng(d[0]);
                locationInfo.setLat(d[1]);
                results.add(locationInfo);
            }
        }
        return results;
    }
    /**
     * 初始化查询是否有轨迹信息
     * @Param [orderId, createTime]
     * @Return cc.yugu.yhdw.common.entity.json.ExtResult
     * @Author zxj 2018/7/10
     */
    public ExtResult findAllMapCount(String orderId, Date createTime) {
        String suffix = DateFormatUtils.format(createTime, "yyyyww");
        if (StringUtils.isNotEmpty(batteryOrderBatteryReportLogMapper.findTableExist(ExchangeOrderBatteryReportLog.EXCHANGE_ORDER_BATTERY_REPORT_LOG + suffix))) {
            int total = batteryOrderBatteryReportLogMapper.findAllMapCount(orderId, suffix, null, null);
            if (total > 0) {
                return ExtResult.successResult();
            }
        }
        return ExtResult.failResult("");
    }
    /**
     * 刷新表格
     * @Param [search]
     * @Return cc.yugu.yhdw.common.entity.pagination.Page
     * @Author zxj 2018/7/10
     */
    public Page findPage(ExchangeOrderBatteryReportLog search) {
        Page page = search.buildPage();
        if (StringUtils.isNotEmpty(batteryOrderBatteryReportLogMapper.findTableExist(ExchangeOrderBatteryReportLog.EXCHANGE_ORDER_BATTERY_REPORT_LOG + search.getSuffix()))) {
            page.setTotalItems(batteryOrderBatteryReportLogMapper.findPageCount(search));
            search.setBeginIndex(page.getOffset());
            List<ExchangeOrderBatteryReportLog> list = batteryOrderBatteryReportLogMapper.findPageResult(search);
            page.setResult(list);
        }
        return page;
    }
    /**
     * 修改地址信息
     * @Param [batteryOrderBatteryReportLog]
     * @Return cc.yugu.yhdw.common.entity.json.ExtResult
     * @Author zxj 2018/7/10
     */
    public ExtResult updateAddress(ExchangeOrderBatteryReportLog batteryOrderBatteryReportLog) {
        int total = batteryOrderBatteryReportLogMapper.updateAddress(batteryOrderBatteryReportLog);
        if (total > 0) {
            return ExtResult.successResult();
        }
        return ExtResult.failResult("修改位置失败");
    }
}
